package com.github.why168;

import com.sun.tools.internal.ws.processor.model.jaxb.JAXBElementMember;

import java.net.SocketTimeoutException;
import java.util.Arrays;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class ArrayList {

    public static void main(String[] args) {
        java.util.ArrayList<Integer> jdkArrayList = new java.util.ArrayList<Integer>();
        jdkArrayList.add(1);
        jdkArrayList.get(0);
        jdkArrayList.remove(0);
        jdkArrayList.trimToSize();

        ArrayList arrayList = new ArrayList();
        arrayList.add("element00");
        arrayList.add("element01");
        arrayList.add(0, "element000");
        arrayList.add("element02");
        System.out.println("-----------------");
        for (int i = 0; i < arrayList.size; i++) {
            System.out.println(arrayList.get(i));
        }

        arrayList.remove(1);
        System.out.println("-----------------");
        for (int i = 0; i < arrayList.size; i++) {
            System.out.println(arrayList.get(i));
        }
        arrayList.remove("element02");
        System.out.println("-----------------");
        for (int i = 0; i < arrayList.size; i++) {
            System.out.println(arrayList.get(i));
        }
    }

    // 方法
    transient Object[] elementData;
    protected transient int modCount = 0;
    private static final int DEFAULT_CAPACITY = 10;
    private int size;
    private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;


    public ArrayList() {
        this.elementData = new Object[DEFAULT_CAPACITY];
    }

    public ArrayList(Object[] elementData) {
        this.elementData = elementData;
    }


    private Object get(int index) {
        rangeCheck(index);

        return elementData[index];
    }

    private void remove(String element) {
        modCount++;
        for (int i = 0; i < size; i++) {
            if (elementData[i].equals(element)) {
//                System.arraycopy(elementData, i + 1, elementData, i, size - i - 1);
                for (int j = i; j < size - 1; j++) {
                    elementData[j] = elementData[j + 1];
                }
                elementData[size - 1] = null;
                size--;
                return;
            }
        }
    }

    private void remove(int index) {
        rangeCheck(index);
        modCount++;

        for (int i = index; i < size - 1; i++) {
            elementData[i] = elementData[i + 1];
        }
        elementData[size - 1] = null;
        size--;
    }

    private void add(int index, String element) {
        rangeCheckForAdd(index);

        ensureCapacityInternal(size + 1);
        System.arraycopy(elementData, index, elementData, index + 1, size - index);
        elementData[index] = element;
        size++;
    }

    private void add(String element) {
        ensureExplicitCapacity(size + 1);
        elementData[size++] = element;
    }

    public void ensureExplicitCapacity(int minCapacity) {
        ensureCapacityInternal(minCapacity);
    }

    private void ensureCapacityInternal(int minCapacity) {
        modCount++;

        if (minCapacity - elementData.length > 0) {
            grow(minCapacity);
        }
    }

    private void grow(int minCapacity) {
        int oldCapacity = elementData.length;
        int newCapacity = oldCapacity + (oldCapacity >> 1);
        if (newCapacity - minCapacity < 0) {
            newCapacity = minCapacity;
        }
        if (newCapacity - MAX_ARRAY_SIZE > 0) {
            throw new OutOfMemoryError();
        }
        elementData = Arrays.copyOf(elementData, newCapacity);
    }


    private void rangeCheck(int index) {
        if (index >= size) {
            throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size);
        }
    }

    private void rangeCheckForAdd(int index) {
        if (index > size || index < 0) {
            throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size);
        }
    }

}
